package com.oraro.paygatewaybiz.module.controller;

import com.github.wxpay.sdk.WXPayUtil;
import com.oraro.middleservice.logger.starter.supports.BizLoggerHelper;
import com.oraro.paygatewaybiz.constant.Constants;
import com.oraro.paygatewaybiz.exception.WXNotifyException;
import com.oraro.paygatewaybiz.proxy.PayChannelServiceProxy;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Author: jianghuiyong
 * @Date: 2018/11/29 21:06
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/pay")
public class NotifyController {

    @Autowired
    private PayChannelServiceProxy payChannelServiceProxy;
    @Autowired
    private BizLoggerHelper bizLoggerHelper;

    /**
     * 微信回调通知接口
     * @param httpServletRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/wxpay/notify")
    public String WXhandleNotify (HttpServletRequest httpServletRequest){

        String xmlResult = null;
        Map<String, String> returnData = new HashMap<>();
        try {
            xmlResult = IOUtils.toString(httpServletRequest.getInputStream(), httpServletRequest.getCharacterEncoding());
        } catch (IOException e) {
            throw new WXNotifyException("XML读取错误");
        }
        bizLoggerHelper.log("=======================微信通知开始========================\n微信回调通知接口请求参数:" + xmlResult);
        Map<String, String> reqMap = new HashMap<>();
        try {
            reqMap = WXPayUtil.xmlToMap(xmlResult);
        } catch (Exception e) {
            throw new WXNotifyException("XML解析错误");
        }
        String notifyResult = payChannelServiceProxy.handleNotify(reqMap, Constants.PayChannelType.WXPAY.getType());
        bizLoggerHelper.log("微信回调通知接口返回结果:" + notifyResult + "\n=======================微信通知结束========================");
        return notifyResult;
    }

    @RequestMapping("/alipay/notify")
    public String AliPayNotify(HttpServletRequest httpServletRequest){
        //获取支付宝POST过来反馈信息
        Map<String,String> params = new HashMap<String,String>();
        Map requestParams = httpServletRequest.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == values.length - 1) ? valueStr + values[i]
                        : valueStr + values[i] + ",";
            }
            //乱码解决，这段代码在出现乱码时使用。如果mysign和sign不相等也可以使用这段代码转化
//            valueStr = new String(valueStr.getBytes("ISO-8859-1"), Constants.UTF8);
            params.put(name, valueStr);
        }
        bizLoggerHelper.log("=======================支付宝通知开始========================\n支付宝回调通知接口请求参数:" + params.toString());
        String notifyResult = payChannelServiceProxy.handleNotify(params, Constants.PayChannelType.ALIPAY.getType());
        bizLoggerHelper.log("支付宝回调通知接口返回结果:" + params.toString() + "\n=======================支付宝通知结束========================");
        return notifyResult;
    }

}
